﻿
namespace DevOnServer
{
    public enum ItemType
    {
        None = 0,
        Inventory = 1
    }

    public class CItem
    {
        public int Count;

        public CItemProp Prop;

        public int MissingCountToMaximum
        {
            get
            {
                return Prop.StackSize - Count;
            }
        }
    }

    public partial class CDatabase
    {
        public void loadInventory(CInventory inventory)
        {
            CQueryResult result = m_dbc.result(
                "SELECT * FROM items WHERE characterid='{0}' AND type='{1}';",
                inventory.Owner.Player.Character.ID, (int)ItemType.Inventory);

            if (result == null)
                return;

            CItem item;
            while (result.advance())
            {
                item = new CItem();

                item.Count = result.read<int>("count");

                item.Prop = m_server.DataManager.ItemProperties[result.read<int>("itemid")];

                inventory.Slots[result.read<int>("place")] = item;
            }

            result.Dispose();
        }

        public void saveInventory(CInventory inventory)
        {
            int charId = inventory.Owner.Player.Character.ID;

            m_dbc.query("DELETE FROM items WHERE characterid='{0}';", charId);

            CItem item;
            for (int i = 0; i < 73; i++)
            {
                item = inventory.Slots[i];

                if (item != null)
                {
                    m_dbc.query("INSERT INTO items(characterid, place, itemid, count) VALUES('{0}', '{1}', '{2}', '{3}');",
                      charId, i, item.Prop.ID, item.Count);
                }
            }
        }
    }
}